package user;
import java.sql.*;

public class Adverticement {

	//hard code the administrator to have ID 1
	public static String SetAdministratorAdverticement(String adv_content)
	{	
		StringBuffer sBuf = new StringBuffer();
		DBConnector db = DBConnector.getDBConnector();	
		String SetAdvStatement = "update administrator set adm_adv = '" + adv_content +"'";
		db.prepareStatement(SetAdvStatement);
		db.updatePstmt();
		System.out.println("Saving the adverticement into the database");
		sBuf.append("<script>alert(unescape('The adverticement has been saved.'))</script>");
		return sBuf.toString();
	}	
	
	public static String GetAdministratorAdverticement()
	{
		StringBuffer sBuf = new StringBuffer();
		DBConnector db = DBConnector.getDBConnector();	
		String GetAdvStatement = "select adm_adv from administrator";
		db.prepareStatement(GetAdvStatement);
		ResultSet adv_content = db.queryPstmt();
		try {
			if (adv_content != null && adv_content.next())
			{
				String adverticement = adv_content.getString(1);
				//if there are no adverticement content
				if (adverticement == null || adverticement.length() == 0 )
					sBuf.append("");
				else
					sBuf.append(adverticement);
			}
		} catch (SQLException e) {
			System.out.println("Adverticement.GetAdministratorAdverticement exception");
			e.printStackTrace();
		}	
		return sBuf.toString();
	}
	
	public static String SetRestaurantAdverticement(String rest_content, String RestID)
	{
		StringBuffer sBuf = new StringBuffer();
		DBConnector db = DBConnector.getDBConnector();	
		String SetRestStatement = "update restaurant set rest_adv = '" + rest_content + "' where rest_ID = " + RestID;
		db.prepareStatement(SetRestStatement);
		db.updatePstmt();
		System.out.println("Saving the adverticement into the database");
		sBuf.append("<script>alert(unescape('The adverticement has been saved.'))</script>");
		return sBuf.toString();
	}
	
	public static String GetRestaurantAdverticement()
	{
		String RestID = RestaurantQuery.getRestaurantSelected();
		StringBuffer sBuf = new StringBuffer();
		DBConnector db = DBConnector.getDBConnector();	
		String GetRestStatement = "select rest_adv from restaurant where rest_ID = " + RestID;
		db.prepareStatement(GetRestStatement);
		ResultSet adv_content = db.queryPstmt();
		try {
			if (adv_content != null && adv_content.next())
			{
				String adverticement = adv_content.getString(1);
				//if there are no adverticement content
				if (adverticement == null || adverticement.length() == 0 )
					sBuf.append("");
				else
					sBuf.append(adverticement);
			}
		} catch (SQLException e) {
			System.out.println("Adverticement.GetRestaurantAdverticement exception");
			e.printStackTrace();
		}	
		return sBuf.toString();
	}
}
